/**
 * Toast 提示组件
 * 使用方法: 
 *   showToast('提示信息');
 *   showToast('操作成功', 'success');
 *   showToast('操作失败', 'error');
 *   showToast('提示信息', 'info', 3000);
 * 
 * @param {string} message - 提示信息
 * @param {string} type - 类型: 'success'(成功), 'error'(失败), 'info'(普通), 默认'info'
 * @param {number} duration - 显示时长（毫秒），默认2000ms
 */
(function(window) {
	'use strict';
	
	// 注入 Toast 样式
	function injectToastStyle() {
		if (document.getElementById('toast-style')) return;
		var style = document.createElement('style');
		style.id = 'toast-style';
		style.textContent = 
			'.toast-container{ position:fixed; top:50%; left:50%; transform:translate(-50%,-50%); z-index:10000; pointer-events:none; }' +
			'.toast{ background:rgba(0,0,0,0.75); color:#fff !important; padding:0.16rem 0.24rem; border-radius:0.08rem; font-size:0.14rem; line-height:1.5; min-width:1.2rem; text-align:center; box-shadow:0 0.04rem 0.12rem rgba(0,0,0,0.15); opacity:0; transform:translateY(-0.2rem); transition:opacity 0.3s, transform 0.3s; display:flex; flex-direction:column; align-items:center; gap:0.08rem; }' +
			'.toast.show{ opacity:1; transform:translateY(0); }' +
			'.toast-icon{ width:0.4rem; height:0.4rem; margin:0 auto 0.04rem; display:flex; align-items:center; justify-content:center; }' +
			'.toast-icon svg{ width:100%; height:100%; }' +
			'.toast-message{ white-space:nowrap; color:#fff !important; }';
		document.head.appendChild(style);
	}
	
	// 获取图标 SVG
	function getIcon(type) {
		var icons = {
			success: '<svg viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="24" cy="24" r="20" stroke="#4ade80" stroke-width="3" fill="none"/><path d="M14 24l8 8 16-16" stroke="#4ade80" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/></svg>',
			error: '<svg viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="24" cy="24" r="20" stroke="#f87171" stroke-width="3" fill="none"/><path d="M18 18l12 12M30 18l-12 12" stroke="#f87171" stroke-width="3" stroke-linecap="round"/></svg>',
			info: ''
		};
		return icons[type] || icons.info;
	}
	
	// Toast 提示函数（兼容旧版本调用方式）
	window.showToast = function(message, typeOrDuration, duration) {
		// 参数处理：兼容 showToast(message, duration) 和 showToast(message, type, duration)
		var type = 'info';
		var finalDuration = 2000;
		
		if (typeof typeOrDuration === 'number') {
			// 旧版本调用: showToast(message, duration)
			finalDuration = typeOrDuration;
		} else if (typeof typeOrDuration === 'string') {
			// 新版本调用: showToast(message, type, duration)
			type = typeOrDuration;
			finalDuration = duration || 2000;
		}
		
		// 注入样式
		injectToastStyle();
		
		// 创建 Toast 元素
		var toast = document.createElement('div');
		toast.className = 'toast';
		
		// 添加图标
		var icon = getIcon(type);
		if (icon) {
			var iconDiv = document.createElement('div');
			iconDiv.className = 'toast-icon';
			iconDiv.innerHTML = icon;
			toast.appendChild(iconDiv);
		}
		
		// 添加文字
		var messageDiv = document.createElement('div');
		messageDiv.className = 'toast-message';
		messageDiv.textContent = message;
		toast.appendChild(messageDiv);
		
		// 获取或创建容器
		var container = document.getElementById('toast-container');
		if (!container) {
			container = document.createElement('div');
			container.id = 'toast-container';
			container.className = 'toast-container';
			document.body.appendChild(container);
		}
		
		// 清空之前的 Toast
		container.innerHTML = '';
		container.appendChild(toast);
		
		// 显示动画
		setTimeout(function() {
			toast.classList.add('show');
		}, 10);
		
		// 隐藏动画
		setTimeout(function() {
			toast.classList.remove('show');
			setTimeout(function() {
				if (container.parentNode && toast.parentNode) {
					container.removeChild(toast);
				}
			}, 300);
		}, finalDuration);
	};
	
})(window);

